<template>
  <div class="container">
    <div class="app-container">
      <!-- Tree 树形控件 -->
      <el-tree
        :data="list"
        :props="{ label: 'name'}"
        default-expand-all
        :expand-on-click-node="false"
      >
        <!-- Tree 树形控件插槽 -->
        <template #default="{ data }">
          <!-- el-row layout布局 -->
          <el-row type="flex" justify="space-between" style="width:100%">
            <el-col>{{ data.name }}</el-col>
            <el-col style="width:160px;text-align:left;">
              <span style="margin: 0 20px">{{ data.managerName }}</span>
              <!-- Dropdown 下拉菜单 -->
              <!-- 监听事件方法 1 el-dropdown监听@command el-dropdown-item添加command参数 -->
              <el-dropdown @command="fn($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                  <!-- 监听事件方法 2  @click.native -->
                  <!-- <el-dropdown-item @click.native="fn('add')">添加子部门</el-dropdown-item>
                  <el-dropdown-item @click.native="fn('edit',data.id)">编辑部门</el-dropdown-item>
                  <el-dropdown-item @click.native="fn('del',data.id)">删除</el-dropdown-item> -->
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 父组件监听 onSuccess 调用获取数据方法重新刷新数据  -->
    <add-dep ref="myAddDep" :visible.sync="visible" :pid="pid" @onSuccess="onload" />
  </div>
</template>
<script>
import { delDepAPI, getDepartmentAPI } from '@/api/department'
import convertToTree from '@/utils/data-conversion'
import addDep from './components/add-dep.vue'
export default {
  name: 'Department',
  components: { addDep },
  data() {
    return {
      list: [],
      visible: false,
      pid: null
    }
  },
  async created() {
    this.onload()
  },
  methods: {
    // 封装获取获取部门列表函数
    async onload() {
      const result = await getDepartmentAPI()
      console.log(result.data)
      // 调用转换数据函数
      this.list = convertToTree(result.data)
      console.log(this.list)
    },
    async fn(type, id) {
      console.log('type,id  --->', type, id)
      if (type === 'add') {
        // 添加 弹窗
        this.visible = true
        this.pid = id // 传递id
      }
      if (type === 'edit') {
        // 编辑 弹窗
        this.visible = true
        // 点击编辑 通过ref 获取add-dep实例对象 调用onEdit方法
        // 提前存入formData.id 解决闪一闪
        this.$refs.myAddDep.formData.id = id
        this.$refs.myAddDep.onEdit(id)
      }
      if (type === 'del') {
        // 删除
        await delDepAPI(id) // 调用删除请求
        this.$message.success('删除成功') // 提示用户
        this.onload() // 重新获取数据
      }
    }
  }
}
</script>
<style lang='scss' scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
